import '../model/comment_model.dart';

/// 评论默认数据
/// 提供默认的评论数据，包含随机点赞数，点踩数统一为0
class CommentData {
  /// 获取所有默认评论数据
  static List<CommentModel> getDefaultComments() {
    final now = DateTime.now();

    return [
      // Post 1 的评论
      CommentModel(
        id: '1',
        userId: '2', // gardenmaster
        postId: '1',
        content:
            'Great start! I love pothos plants too. Looking forward to seeing your progress updates!',
        commentTime: now.subtract(const Duration(days: 4, hours: 2)),
        likeCount: 8,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '2',
        userId: '3', // leafyfriend
        postId: '1',
        content:
            'Pothos are so easy to care for. Make sure to let the soil dry out between waterings!',
        commentTime: now.subtract(const Duration(days: 3, hours: 5)),
        likeCount: 12,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '3',
        userId: '4', // plantcare
        postId: '1',
        content:
            'I\'ve been growing pothos for years. They\'re perfect for beginners!',
        commentTime: now.subtract(const Duration(days: 2, hours: 8)),
        likeCount: 6,
        dislikeCount: 0,
      ),

      // Post 2 的评论
      CommentModel(
        id: '4',
        userId: '1', // admin
        postId: '2',
        content: 'Amazing transformation! Your herbs look so healthy now.',
        commentTime: now.subtract(const Duration(days: 2, hours: 3)),
        likeCount: 15,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '5',
        userId: '5', // greensoul
        postId: '2',
        content:
            'I had the same problem with my herbs last winter. Great tips!',
        commentTime: now.subtract(const Duration(days: 1, hours: 12)),
        likeCount: 9,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '6',
        userId: '6', // blooming
        postId: '2',
        content:
            'The video is so helpful! I\'m going to try this with my mint.',
        commentTime: now.subtract(const Duration(hours: 18)),
        likeCount: 11,
        dislikeCount: 0,
      ),

      // Post 3 的评论
      CommentModel(
        id: '7',
        userId: '7', // plantlife
        postId: '3',
        content: 'Such a creative idea! I love the eco-friendly approach.',
        commentTime: now.subtract(const Duration(days: 1, hours: 6)),
        likeCount: 7,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '8',
        userId: '8', // naturelover
        postId: '3',
        content:
            'I\'ve been looking for ways to upcycle bottles. This is perfect!',
        commentTime: now.subtract(const Duration(hours: 14)),
        likeCount: 13,
        dislikeCount: 0,
      ),

      // Post 4 的评论
      CommentModel(
        id: '9',
        userId: '1', // admin
        postId: '4',
        content:
            'Beautiful succulent arrangement! The colors complement each other perfectly.',
        commentTime: now.subtract(const Duration(hours: 20)),
        likeCount: 10,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '10',
        userId: '2', // gardenmaster
        postId: '4',
        content:
            'I love how you\'ve arranged them by height. Great visual impact!',
        commentTime: now.subtract(const Duration(hours: 16)),
        likeCount: 5,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '11',
        userId: '3', // leafyfriend
        postId: '4',
        content: 'Succulents are my favorite! What\'s your watering schedule?',
        commentTime: now.subtract(const Duration(hours: 10)),
        likeCount: 8,
        dislikeCount: 0,
      ),

      // Post 5 的评论
      CommentModel(
        id: '12',
        userId: '4', // plantcare
        postId: '5',
        content:
            'Basil is such a resilient herb. Great job bringing it back to life!',
        commentTime: now.subtract(const Duration(hours: 8)),
        likeCount: 6,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '13',
        userId: '6', // blooming
        postId: '5',
        content:
            'I had the same pest problem last year. Your solution worked perfectly!',
        commentTime: now.subtract(const Duration(hours: 4)),
        likeCount: 9,
        dislikeCount: 0,
      ),

      // Post 6 的评论
      CommentModel(
        id: '14',
        userId: '1', // admin
        postId: '6',
        content:
            'This is absolutely amazing! I never thought of using old tires this way.',
        commentTime: now.subtract(const Duration(hours: 6)),
        likeCount: 18,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '15',
        userId: '3', // leafyfriend
        postId: '6',
        content:
            'The video tutorial is fantastic! I\'m definitely trying this.',
        commentTime: now.subtract(const Duration(hours: 3)),
        likeCount: 14,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '16',
        userId: '5', // greensoul
        postId: '6',
        content:
            'Such a creative way to recycle! Your garden must look incredible.',
        commentTime: now.subtract(const Duration(hours: 1)),
        likeCount: 7,
        dislikeCount: 0,
      ),

      // Post 7 的评论
      CommentModel(
        id: '17',
        userId: '2', // gardenmaster
        postId: '7',
        content:
            'Hydroponics is fascinating! I love how you can see the roots growing.',
        commentTime: now.subtract(const Duration(hours: 2)),
        likeCount: 11,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '18',
        userId: '8', // naturelover
        postId: '7',
        content:
            'I\'ve been wanting to try hydroponics. This is very inspiring!',
        commentTime: now.subtract(const Duration(hours: 1, minutes: 30)),
        likeCount: 8,
        dislikeCount: 0,
      ),

      // Post 8 的评论
      CommentModel(
        id: '19',
        userId: '4', // plantcare
        postId: '8',
        content:
            'Thyme is such a wonderful herb. Great job with the winter care!',
        commentTime: now.subtract(const Duration(minutes: 45)),
        likeCount: 12,
        dislikeCount: 0,
      ),
      CommentModel(
        id: '20',
        userId: '7', // plantlife
        postId: '8',
        content: 'I can\'t wait for spring too! Your thyme looks so healthy.',
        commentTime: now.subtract(const Duration(minutes: 20)),
        likeCount: 5,
        dislikeCount: 0,
      ),
    ];
  }

  /// 根据帖子ID获取评论
  static List<CommentModel> getCommentsByPostId(String postId) {
    return getDefaultComments()
        .where((comment) => comment.postId == postId)
        .toList()
      ..sort((a, b) => b.commentTime.compareTo(a.commentTime));
  }

  /// 根据用户ID获取评论
  static List<CommentModel> getCommentsByUserId(String userId) {
    return getDefaultComments()
        .where((comment) => comment.userId == userId)
        .toList()
      ..sort((a, b) => b.commentTime.compareTo(a.commentTime));
  }

  /// 获取评论统计信息
  static Map<String, int> getCommentStatistics() {
    final comments = getDefaultComments();
    final stats = <String, int>{};

    // 总评论数
    stats['totalComments'] = comments.length;

    // 总点赞数
    stats['totalLikes'] = comments.fold(
      0,
      (sum, comment) => sum + comment.likeCount,
    );

    // 总点踩数
    stats['totalDislikes'] = comments.fold(
      0,
      (sum, comment) => sum + comment.dislikeCount,
    );

    // 平均点赞数
    stats['averageLikes'] =
        comments.isNotEmpty
            ? (stats['totalLikes']! / comments.length).round()
            : 0;

    return stats;
  }

  /// 根据ID获取评论
  static CommentModel? getCommentById(String id) {
    try {
      return getDefaultComments().firstWhere((comment) => comment.id == id);
    } catch (e) {
      return null;
    }
  }

  /// 获取最受欢迎的评论（按点赞数排序）
  static List<CommentModel> getPopularComments() {
    final comments = getDefaultComments();
    comments.sort((a, b) => b.likeCount.compareTo(a.likeCount));
    return comments;
  }

  /// 获取最新评论
  static List<CommentModel> getLatestComments() {
    final comments = getDefaultComments();
    comments.sort((a, b) => b.commentTime.compareTo(a.commentTime));
    return comments;
  }
}
